/*

Copyright 2009 Nicola Dal Pont

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Version ${MAJOR_VERSION}.${MINOR_VERSION}${BETA_MARK} Build ${BUILD_NUMBER}

*/

package com.astoolkit.workflow.parsley {
    
    
    
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.utils.getDefinitionByName;
    
    import mx.rpc.AsyncToken;
    import mx.rpc.Fault;
    import mx.rpc.events.FaultEvent;
    
    import org.spicefactory.parsley.core.messaging.command.CommandObserverProcessor;
    import org.spicefactory.parsley.core.messaging.command.CommandStatus;
    import org.spicefactory.parsley.core.messaging.receiver.CommandObserver;
    
    internal class MessageObserver extends EventDispatcher implements CommandObserver
    {
        private var _selector : *;
        private var _processor : CommandObserverProcessor;
        private var _messageType : Class;
        private var _result : Object;
        private var _observedStatus : CommandStatus;
        
        public function MessageObserver( inMessageType : Class, inStatus : CommandStatus )
        {
            _messageType = inMessageType;
            _observedStatus = inStatus;
        }
        
        public function get result() : Object
        {
            return _result;
        }
        
        public function get status () : CommandStatus
        {
            return _observedStatus;
        }
        
        // ------------- CommandObserver implementation -------------------//
        public function observeCommand ( inProcessor : CommandObserverProcessor ) : void
        {
            _processor = inProcessor;
            if( status.key == CommandStatus.COMPLETE.key )
            {
                _result = AsyncToken( inProcessor.command.returnValue ).result;
                
                dispatchEvent( new Event( Event.COMPLETE ) );
            }
            else if( status.key == CommandStatus.ERROR.key )
            {
                dispatchEvent( new FaultEvent( FaultEvent.FAULT, false, false, new Fault( "", _processor.command.returnValue as String ) ) );
            }
        }
        
        // ------------- MessageDispatcher implementation -------------------//
        public function get messageType() : Class
        {
            return _messageType;
        }
        
        public function get order() : int
        {
            return 1;
        }
        
        public function get selector() : *
        {
            return _selector;
        }
        
        public function set selector( inSelector : * ) : void
        {
            _selector = inSelector;
        }
        
        
    }
}